home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / c80tcog.lbr / STRCHR.CQ / strchr.c
Text File  |  1985-08-09  |  5KB  |  193 lines

  1. /* strchr.c - functions for null-terminated strings             */
  2. /*                                                      1982/10/10 12:23
  3.  
  4.         Copyright 1982  William G. Hutchison, Jr.
  5.                         P.O. Box 278
  6.                         Exton, PA 19341-0278
  7.                         U.S.A.
  8.  
  9.                         CompuServe 70665,1307
  10.  
  11.  
  12.     These functions may be used freely for any non-commercial
  13. purpose, provided that the user does  not  remove  or  alter
  14. this notice or the copyright statement.
  15.     Those who wish to  sell  or lease these functions, or to
  16. incorporate them into a product for  sale or  lease,  should
  17. apply to the author (above) for licensing information.
  18.     These functions are not  covered by  a  warranty, either
  19. express or implied. The author shall not be responsible  for
  20. any  damages (including consequential) caused by reliance on
  21. the  materials  presented,  including  but  not  limited  to
  22. typographical errors or arithmetic errors.
  23.  
  24.     NOTE: The names and functions of these sub-programs  are
  25. the  same  as  certain  sub-programs provided  with the UNIX
  26. system (tm Western Electric Co.), but these sub-programs are
  27. original work, not copies of the UNIX sub-programs.
  28.  
  29.  */
  30.  
  31. /*      definitions for Software Toolworks C/80 Version 2.0:    */
  32. #ifdef MAINLY
  33. #else
  34. #include "c80def.h"
  35. #endif
  36.  
  37. /* index - return pointer to c in s, or NULL if not found       */
  38.  
  39. #ifdef UNIX
  40. char *
  41. #endif
  42. index(s, c) char *s, c;
  43. {
  44.         while(*s && *s != c) s++; 
  45.         return(*s? s : NULL);
  46. }                /* end index */
  47.  
  48. /* rindex - like index, but search s right-to-left              */
  49.  
  50. #ifdef UNIX
  51. char *
  52. #endif
  53. rindex(s, c) char *s, c;
  54. {
  55.         register int i; 
  56.         for(i= strlen(s)-1; i >= 0 && c != s[i]; i--)
  57.                 ;
  58.         return (i < 0? NULL : s+i);
  59. }                /* end rindex */
  60.  
  61. /* strcat - concatenate s2 onto end of s1, return s1            */
  62.  
  63. #ifdef UNIX
  64. char *
  65. #endif
  66. strcat(s1, s2) char *s1, *s2;
  67. {
  68.         return(strcpy(s1+strlen(s1), s2));
  69. }                /* end strcat */
  70.  
  71. /* strcmp - compare s1 vs. s2 in native code order. Return -1 if
  72. s1 < s2, +1 if s2 > s1, 0 otherwise. */
  73.  
  74. #ifdef UNIX
  75. int
  76. #endif
  77. strcmp(s1, s2) char *s1, *s2;
  78. {
  79.         for (; *s1 && *s1 == *s2; s1++, s2++)
  80.                 ;
  81.         return(*s1 < *s2? -1 : (*s1 > *s2? 1 : 0));
  82. }                /* end strcmp */
  83.  
  84. /* strcpy - copy s2 to s1, return s1 */
  85.  
  86. #ifdef UNIX
  87. char *
  88. #endif
  89. strcpy(s1, s2) char *s1, *s2;
  90. {
  91.         char *s;
  92.         s= s1;
  93.         while(*s1++ = *s2++)
  94.                 ;
  95.         return(s);
  96. }                /* end strcpy */
  97.  
  98. /* strlen - return length of string */
  99.  
  100. #ifdef UNIX
  101. int
  102. #endif
  103. strlen(s) char *s;
  104. {
  105.         register char *t; 
  106.         for(t=s; *t; t++)
  107.                 ;
  108.         return(t-s);
  109. }                /* end strlen */
  110.  
  111. /* strncat - concatenate at most n characters from s2 onto end of s1;
  112. return s1       */
  113.  
  114. #ifdef UNIX
  115. char *
  116. #endif
  117. strncat(s1, s2, n) char *s1, *s2; 
  118. int n;
  119. {
  120.         return(strncpy(s1+strlen(s1), s2, n));
  121. }                /* end strncat */
  122.  
  123. /* strncmp - compare at most n characters of s1 vs. s2 in native
  124. code order. Return -1 if s1 < s2, +1 if s2 > s1, 0 otherwise. */
  125.  
  126. #ifdef UNIX
  127. int
  128. #endif
  129. strncmp(s1, s2, n) char *s1, *s2; 
  130. int n;
  131. {
  132.         for (; n-- > 0 && *s1 && *s1 == *s2; s1++, s2++)
  133.         ;
  134.         return(*s1 < *s2? -1 : (*s1 > *s2? 1 : 0));
  135. }                /* end strncmp */
  136.  
  137. /* strncpy - copy at most n characters from s2 to s1, return s1 */
  138.  
  139. #ifdef UNIX
  140. char *
  141. #endif
  142. strncpy(s1, s2, n) char *s1, *s2; 
  143. int n;
  144. {
  145.         char *s;
  146.         s= s1;
  147.         while(n-- > 0 && (*s1++ = *s2++))
  148.                 ;
  149.         return(s);
  150. }                /* end strncpy */
  151.  
  152. /* strpbrk - returns a pointer to the first occurrence in string s1
  153. of any character from string s2, or NULL if no character from s2
  154. exists in s1. */
  155.  
  156. #ifdef UNIX
  157. char *
  158. #endif
  159. strpbrk(s1, s2) char *s1, *s2;
  160. {
  161.         for(;*s1; s1++)
  162.                 if(index(s2, *s1) != NULL) return(s1);
  163.         return(NULL);
  164. }                /* end strpbrk */
  165.  
  166. /* strspn - returns the length of the initial segment of string s1
  167. which consists entirely of characters from string s2. */
  168.  
  169. #ifdef UNIX
  170. int
  171. #endif
  172. strspn(s1, s2) char *s1, *s2;
  173. {
  174.         register char *t;
  175.         for(t= s1; *t && strpbrk(t, s2) != NULL; t++)
  176.                 ;
  177.         return(t-s1);
  178. }                /* end strspn */
  179.  
  180. /* strcspn - returns the length of the initial segment of string s1
  181. which consists entirely of characters not in string s2. */
  182.  
  183. #ifdef UNIX
  184. int
  185. #endif
  186. strcspn(s1, s2) char *s1, *s2;
  187. {
  188.         register char *t;
  189.         for(t= s1; *t && strpbrk(t, s2) == NULL; t++)
  190.                 ;
  191.         return(t-s1);
  192. }                /* end strcspn */
  193.